package com.fow.core.platform.sms.ali;

/**
 * Created by suggestion on 2016/1/21.
 */

import com.fow.core.platform.json.JSONBinder;
import com.fow.core.platform.sms.ali.parameters.CaptchaMsg;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class MsgSenderService {

    private static final String URL = "http://gw.api.taobao.com/router/rest";//环境调用地址

    private String appkey;

    private String secret;

    private String sms_free_sign_name;

    private String sms_template_code;

    private String productName;

    public void setAppkey(String appkey) {
        this.appkey = appkey;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public void setSms_free_sign_name(String sms_free_sign_name) {
        this.sms_free_sign_name = sms_free_sign_name;
    }

    public void setSms_template_code(String sms_template_code) {
        this.sms_template_code = sms_template_code;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getProductName() {
        return productName;
    }

    private <M> String msgBuilder(String phone, M message) {

        TreeMap<String, Object> apiparamsMap = new TreeMap<String, Object>();

        apiparamsMap.put("format", "json");

        apiparamsMap.put("method", "alibaba.aliqin.fc.sms.num.send");

        apiparamsMap.put("sign_method", "md5");

        apiparamsMap.put("app_key", appkey);

        apiparamsMap.put("v", "2.0");

        String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

        apiparamsMap.put("timestamp", timestamp);

        apiparamsMap.put("sms_type", "normal");
        apiparamsMap.put("rec_num", phone);


        apiparamsMap.put("sms_free_sign_name", sms_free_sign_name);
        apiparamsMap.put("sms_template_code", sms_template_code);

        apiparamsMap.put("sms_param", JSONBinder.binder((Class<M>) message.getClass()).toJSON(message));
        //生成签名

        String sign = MsgSenderUtils.md5Signature(apiparamsMap, secret);

        apiparamsMap.put("sign", sign);

        StringBuilder param = new StringBuilder();

        for (Iterator<Map.Entry<String, Object>> it = apiparamsMap.entrySet()

                .iterator(); it.hasNext(); ) {

            Map.Entry<String, Object> e = it.next();

            param.append("&").append(e.getKey()).append("=").append(e.getValue().toString());

        }

        return param.toString().substring(1);
    }

    public void send(String phone,Object message) {
        MsgSenderUtils.getResult(URL, msgBuilder(phone, message));
    }

    public String sendCaptcha(String phone){
        String captcha = this.getCaptcha();
        CaptchaMsg msg = new CaptchaMsg(captcha,this.productName);

        this.send(phone,msg);

        return captcha;
    }

    public String getCaptcha() {
        String captcha = "";
        for (int i = 0; i < 6; i++) {
            captcha += (int) (Math.random() * 10);
        }
        return captcha;
    }
}


